﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BussinessLayer;

namespace projectQLDV
{
    public partial class frmDoanVien : DevExpress.XtraEditors.XtraForm
    {
        private frmMain frmM;
        private int idDV;
        private string namhoc;
        private int khoi;
        private string lop;
        private DataTable dt;
        public frmDoanVien(frmMain frm, string _nh)
        {
            InitializeComponent();
            frmM = frm;
            idDV = -1;
            namhoc = _nh;
            formLoad();
        }

        public frmDoanVien(frmMain frm, int id, string _nh)
        {
            InitializeComponent();
            frmM = frm;
            idDV = id;
            formLoad();
            
        }

        private void formLoad()
        {
            tblchidoan cd = new tblchidoan();
            this.dt = new DataTable();
            this.dt = cd.SelectCDTheoNamHoc(this.namhoc);
            foreach (DataRow r in this.dt.Rows)
            {
                this.comboBoxEditChiDoan.Properties.Items.Add(r["khoi"].ToString() + r["lop"].ToString());
            }
            this.comboBoxEditChiDoan.SelectedIndex = 0;
        }

        private void frmDoanVien_FormClosed(object sender, FormClosedEventArgs e)
        {
            if(idDV == -1)
                frmM.frmThemDV_isShown = false;
            else frmM.frmCapNhatDV_isShown = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            string _hoten = this.textEditHoTen.Text;
            DateTime _ngaysinh = DateTime.Parse(this.dateEditNgaySinh.Text).Date;
            bool _gioitinh = this.checkEditNam.Checked;
            string _noisinh = this.textEditNoiSinh.Text;
            string _dantoc = this.textEditDanToc.Text;
            string _tongiao = this.textEditTonGiao.Text;
            bool _lahocsinh = this.checkEditLaHocSinh.Checked;
            bool _trangthai = true;
            tblHocsinh_giaovien hsgv = new tblHocsinh_giaovien();
            tblhocsinh_chidoan hscd = new tblhocsinh_chidoan();

            try
            {
                hsgv.hocsinh_giaovien(this.idDV, _hoten, _ngaysinh, _gioitinh, _noisinh, _dantoc, _tongiao, _lahocsinh, _trangthai);
                
                if (this.checkEditLaDoanVien.Checked)
                {
                    tblDoanvien dv = new tblDoanvien();
                    DateTime _ngayvaodoan = DateTime.Parse(this.dateEditNgayVaoDoan.Text).Date;
                    string _donviketnap = this.textEditDonViKetNap.Text;
                    dv.doanvien(this.idDV, _ngayvaodoan, _donviketnap);                    
                }
                int cdid = int.Parse(this.dt.Rows[this.comboBoxEditChiDoan.SelectedIndex]["chidoan_id"].ToString());
                hscd.hocsinh_chidoan(this.idDV, cdid);
                MessageBox.Show("Thêm thành công!!!");
                frmM.GridViewdoanvien(this.namhoc);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
        //sự kiện check "là đoàn viên" hay không
        private void checkEditLaDoanVien_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.checkEditLaDoanVien.Checked)
            {
                this.dateEditNgayVaoDoan.Enabled = false;
                this.textEditDonViKetNap.Enabled = false;
            }
            else
            {
                this.dateEditNgayVaoDoan.Enabled = true;
                this.textEditDonViKetNap.Enabled = true;
            }
        }

        private void btnLuuVaThoat_Click(object sender, EventArgs e)
        {
            btnLuu_Click(sender, e);
            btnThoat_Click(sender, e);
        }

        private void comboBoxEditChiDoan_SelectedValueChanged(object sender, EventArgs e)
        {
            this.khoi = int.Parse(this.comboBoxEditChiDoan.Text.Substring(0, 2));
            this.lop = this.comboBoxEditChiDoan.Text.Substring(2);
        }
    }
}